package com.bjsxt.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.WebConfig;
import com.bjsxt.model.Result;
import com.bjsxt.service.WebConfigService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;

/**
 * description:
 * 网站资源配置的控制层
 * 网站资源配置的权限:
 * 查询： web_config_query
 * 修改： web_config_update
 * 新增： web_config_create
 * 删除： web_config_delete
 *
 * @author ALIENWARE <br />
 * @date 2021/7/19
 */
@RestController
@RequestMapping("/webConfigs")
@Api(tags = "网站资源配置的控制器")
public class WebConfigController {

    /**
     * 网站资源配置业务层接口
     */
    @Autowired
    private WebConfigService webConfigService;

    /**
     * 分页查询网站资源配置数据
     *
     * @param page 分页对象<WebConfig>
     * @param name 名称
     * @param type 分组
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('web_config_query')")
    @ApiOperation(value = "分页查询网站资源配置数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query", example = "name"),
            @ApiImplicitParam(name = "type", value = "分组", dataType = "String", paramType = "query", example = "LINK_BANNER")
    })
    public Result<Page<WebConfig>> findByPage(@ApiIgnore Page<WebConfig> page, String name, String type) {
        page.addOrder(OrderItem.desc(WebConfig.COL_CREATED));
        // 调用业务层进行分页数据的查询
        Page<WebConfig> webConfigPage = webConfigService.pageByNameAndType(page, name, type);
        // 返回响应数据
        return Result.success(webConfigPage);
    }

    /**
     * 获取PC端的广告图
     *
     * @return 统一响应对象
     */
    @GetMapping("/banners")
    @ApiOperation(value = "分页查询网站资源配置数据", authorizations = {@Authorization("Authorization")})
    public Result<List<WebConfig>> getBanners() {
        // 调用业务层进行分页数据的查询
        List<WebConfig> webConfigs = webConfigService.listByTypeAndStatus("WEB_BANNER", (byte) 1);
        // 返回响应数据
        return Result.success(webConfigs);
    }

    /**
     * 添加网站资源配置
     *
     * @param webConfig 网站资源配置对象
     * @return 统一响应对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('web_config_create')")
    @ApiOperation(value = "添加网站资源配置", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "WebConfig 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> add(@RequestBody @Validated WebConfig webConfig) {
        // 调用业务层方法进行数据的保存
        boolean flag = webConfigService.save(webConfig);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 修改网站资源配置
     *
     * @param webConfig 网站资源配置对象
     * @return 统一响应对象
     */
    @PatchMapping
    @PreAuthorize("hasAuthority('web_config_update')")
    @ApiOperation(value = "修改网站资源配置", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "WebConfig 的 json 数据", dataType = "String", paramType = "body", example = "{}")
    })
    public Result<Void> update(@RequestBody @Validated WebConfig webConfig) {
        // 调用业务层方法进行数据的保存
        boolean flag = webConfigService.updateById(webConfig);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }

    /**
     * 删除网站资源配置
     *
     * @param ids 网站资源配置 id 数组
     * @return 统一响应对象
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('web_config_delete')")
    @ApiOperation(value = "删除网站资源配置", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "WebConfig 的 id 集合", dataType = "Long", required = true, paramType = "body", example = "1")
    })
    public Result<Void> delete(@RequestBody @NotEmpty Long[] ids) {
        // 调用业务层方法进行数据的更新
        boolean flag = webConfigService.removeByIds(Arrays.asList(ids));
        // 如果为 true 删除成功
        if (flag) return Result.success("删除成功");
        // 如果为 false 删除失败
        return Result.fail("删除失败");
    }
}
